//
//  XLAccountManager.m
//  XiangLove
//
//  Created by 陈宪栋 on 2017/11/28.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLAccountManager.h"
#import <YYCache.h>
#import "XLUserInfoModel.h"

#define DIR_NAME F(@"Baomihua.%@.userInfo",[[[NSBundle mainBundle]infoDictionary] objectForKey:(NSString*)kCFBundleNameKey])
static NSString *const BMHUserInfoKey = @"baomihua_userinfo";

@interface XLAccountManager ()
@property (nonatomic, copy) NSString *filePath;
@property (nonatomic, strong) YYCache *cache;
@end

@implementation XLAccountManager
+ (XLAccountManager *)shareInstance {
    static XLAccountManager *manger;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manger = [[XLAccountManager alloc] init];
    });
    return manger;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:DIR_NAME];
        self.cache = [[YYCache alloc] initWithPath:self.filePath];
        _model = (XLUserInfoModel *)[self.cache objectForKey:BMHUserInfoKey];
    }
    return self;
}

- (void)updateUserInfo:(XLUserInfoModel *)model {
    _model = model;
    [self.cache setObject:model forKey:BMHUserInfoKey];
}

- (void)removeUserInfo {
    _model = nil;
    [self.cache removeObjectForKey:BMHUserInfoKey];
}
@end
